<template>
    <div class="default-main ba-table-box">
        <el-alert class="ba-table-alert" v-if="baTable.table.remark" :title="baTable.table.remark" type="info" show-icon />

        <el-card style=" margin: 10px 0;" shadow="hover">
            <el-row>
                <el-col :span="8" style="text-align: center;">
                    <el-statistic :title="t('所有税额')" :value="state.drawback_amount_count" />
                </el-col>
                <el-col :span="8" style="text-align: center;">
                    <el-statistic :title="t('已退税')" :value="state.drawback_amount_count_true" />
                </el-col>
                <el-col :span="8" style="text-align: center;">
                    <el-statistic :title="t('未退税')" :value="state.drawback_amount_count_false" />
                </el-col>
            </el-row>
        </el-card>
        

        <!-- 表格顶部菜单 -->
        <!-- 自定义按钮请使用插槽，甚至公共搜索也可以使用具名插槽渲染，参见文档 -->
        <TableHeader :buttons="[]" :quick-search-placeholder="t('搜索')">

            
            <template #btnBefore>
                <el-select v-model="state.search.year" style="width: 150px;" clearable :placeholder="t('请选择年份')">
                    <el-option label="2021" value="2021" />
                    <el-option label="2022" value="2022" />
                    <el-option label="2023" value="2023" />
                    <el-option label="2024" value="2024" />
                </el-select>
                <el-select v-model="state.search.month" clearable :placeholder="t('请选择月份')" style="width: 150px;" class="ml-5">
                    <el-option label="1" value="1" />
                    <el-option label="2" value="2" />
                    <el-option label="3" value="3" />
                    <el-option label="4" value="4" />
                    <el-option label="5" value="5" />
                    <el-option label="6" value="6" />
                    <el-option label="7" value="7" />
                    <el-option label="8" value="8" />
                    <el-option label="9" value="9" />
                    <el-option label="10" value="10" />
                    <el-option label="11" value="11" />
                    <el-option label="12" value="12" />
                </el-select>
                <el-select v-model="state.search.drawback_status" clearable :placeholder="t('状态')" style="width: 150px;" class="ml-5">
                    <el-option :label="t('发票开具中')" value="2" />
                    <el-option :label="t('发票稽查中')" value="5" />
                    <el-option :label="t('已申请退税')" value="3" />
                    <el-option :label="t('退税完成')" value="4" />
                    
                </el-select>
                <span class="btn ml-5" @click="search">{{ t('搜索') }}</span>
                
                
            </template>
            <!-- <template #btnAfter>
                <div>
                    <el-badge :value="state.drawback_amount_count" class="item">
                        <el-tag type="primary" effect="plain" size="Large" style="font-size: 13px;">{{ t('所有税额') }}</el-tag>
                    </el-badge>
                    <el-badge :value="state.drawback_amount_count_true" class="item" style="margin-left: 75px;">
                        <el-tag type="primary" effect="plain" size="Large" style="font-size: 13px;">{{ t('已退税') }}</el-tag>
                    </el-badge>
                    <el-badge :value="state.drawback_amount_count_false" class="item" style="margin-left: 75px; margin-right: 60px;">
                        <el-tag type="primary" effect="plain" size="Large" style="font-size: 13px;">{{ t('未退税') }}</el-tag>
                    </el-badge>
                    
                </div>
            </template> -->
        </TableHeader>
        
        

        

        <!-- 表格 -->
        <!-- 表格列有多种自定义渲染方式，比如自定义组件、具名插槽等，参见文档 -->
        <!-- 要使用 el-table 组件原有的属性，直接加在 Table 标签上即可 -->
        
        <Table ref="tableRef">
            <template #drawback_rate>
                <!-- 在插槽内，您可以随意发挥，通常使用 el-table-column 组件 -->
                <el-table-column :label="t('退税率')" align="center">
                    <template #default="scope">
                        {{ scope.row.drawback_rate }}%
                    </template>
                </el-table-column>
            </template>
            <template #dk_status>
                <!-- 在插槽内，您可以随意发挥，通常使用 el-table-column 组件 -->
                <el-table-column :label="t('状态')" align="center">
                    <template #default="scope">
                        
                        <el-dropdown>
                            <span class="el-dropdown-link" style="cursor: pointer;">
                                <el-text v-if="scope.row.drawback_status == null || scope.row.drawback_status == ''" type="danger" style="font-size: 13px; color: #8f8f8f; line-height: 23px;">{{ t('无状态') }}</el-text>
                                <el-text v-if="scope.row.drawback_status == 2" type="primary" style="font-size: 13px; color: #8f8f8f; line-height: 23px;">{{ t('发票开具中') }}</el-text>
                                <el-text v-if="scope.row.drawback_status == 5" type="danger" style="font-size: 13px; color: #f89406; line-height: 23px;">{{ t('发票稽查中') }}</el-text>
                                <el-text v-if="scope.row.drawback_status == 3" type="success" style="font-size: 13px; color: #2f96b4; line-height: 23px;">{{ t('已申请退税') }}</el-text>
                                <el-text v-if="scope.row.drawback_status == 4" type="success" style="font-size: 13px; color: #67c23a; line-height: 23px;">{{ t('退税完成') }}</el-text>
                            </span>
                            <template #dropdown>
                            <el-dropdown-menu>
                                <el-dropdown-item @click="changeDrawbackStatus(scope, 2)" :disabled="scope.row.drawback_status == 2">
                                    <span style="border-radius: 1.5px; background: #8f8f8f; width: 5px; height: 15px; margin-right: 12px;"></span>
                                    {{ t('发票开具中') }}
                                </el-dropdown-item>
                                <el-dropdown-item @click="changeDrawbackStatus(scope, 5)" :disabled="scope.row.drawback_status == 1">
                                    <span style="border-radius: 1.5px; background: #f89406; width: 5px; height: 15px; margin-right: 12px;"></span>
                                    {{ t('发票稽查中') }}
                                </el-dropdown-item>
                                <el-dropdown-item @click="changeDrawbackStatus(scope, 3)" :disabled="scope.row.drawback_status == 3">
                                    <span style="border-radius: 1.5px; background: #2f96b4; width: 5px; height: 15px; margin-right: 12px;"></span>
                                    {{ t('已申请退税') }}
                                </el-dropdown-item>
                                <el-dropdown-item @click="changeDrawbackStatus(scope, 4)" :disabled="scope.row.drawback_status == 4">
                                    <span style="border-radius: 1.5px; background: #67c23a; width: 5px; height: 15px; margin-right: 12px;"></span>
                                    {{ t('退税完成') }}
                                </el-dropdown-item>
                            </el-dropdown-menu>
                            </template>
                        </el-dropdown>
                    </template>
                </el-table-column>
            </template>

            <template #optime>
                <!-- 在插槽内，您可以随意发挥，通常使用 el-table-column 组件 -->
                <el-table-column :label="t('操作时间')" align="center" width="180">
                    <template #default="scope">
                        
                        <el-text v-if="scope.row.optime2" type="primary" style="font-size: 13px; color: #8f8f8f; line-height: 23px; display: block;">{{ t('发票开具中') }}：{{ scope.row.optime2 }}</el-text>
                        <el-text v-if="scope.row.optime5" type="danger" style="font-size: 13px; color: #f89406; line-height: 23px; display: block;">{{ t('发票稽查中') }}：{{ scope.row.optime5 }}</el-text>
                        <el-text v-if="scope.row.optime3" type="success" style="font-size: 13px; color: #2f96b4; line-height: 23px; display: block;">{{ t('已申请退税') }}：{{ scope.row.optime3 }}</el-text>
                        <el-text v-if="scope.row.optime4" type="success" style="font-size: 13px; color: #67c23a; line-height: 23px; display: block;">{{ t('退税完成') }}：{{ scope.row.optime4 }}</el-text>
                            
                    </template>
                </el-table-column>
            </template>

            <template #trade_type>
                <!-- 在插槽内，您可以随意发挥，通常使用 el-table-column 组件 -->
                <el-table-column :label="t('订单号')" align="center" width="120">
                    <template #default="scope">
                        <el-link @click="router.push({path: '/system/customer_order/detail', query: {id: scope.row.id}})" style="font-size: 13px;" class="contract">
                            <span style="display: block;" v-if="!scope.row.contract">{{ scope.row.out_trade_no }}</span>
                            <span style="display: block;">{{ scope.row.contract }}</span>
                        </el-link>
                    </template>
                </el-table-column>
            </template>

            
            
        </Table>

        <!-- 表单 -->
        <PopupForm />

        <!-- <Mingxi v-if="state.mingxi.id != 0" :data="state.mingxi" @closeMingxi="closeMingxi" /> -->


    </div>

    
</template>

<script setup lang="ts">
import { ref, provide, onMounted, reactive,markRaw  } from 'vue'
import baTableClass from '/@/utils/baTable'
import { defaultOptButtons } from '/@/components/table'
import { baTableApi } from '/@/api/common'
import { useI18n } from 'vue-i18n'
import PopupForm from './popupForm.vue'
import Table from '/@/components/table/index.vue'
import TableHeader from '/@/components/table/header/index.vue'
import Mingxi from './mingxi.vue'
import { cancelApi } from '/@/api/most/finance/drawback'
import router from '/@/router/index'

import Note from './note.vue';
import Refunded from './refunded.vue';
import { changeDrawbackStatusApi } from '/@/api/most/customer/order'

defineOptions({
    name: 'finance/account/index',
})

const { t } = useI18n()
const tableRef = ref()

const state = reactive({
    search: {
        year: '',
        month: '',
        drawback_status: null,
        type: ''
    },
    drawback_amount_count: null,
    drawback_amount_count_false: null,
    drawback_amount_count_true: null,
    admin: {}
})


const changeDrawbackStatus = (scope: anyObj, status: number) => {
    
    // scope.row.status = status;
    changeDrawbackStatusApi({id: scope.row.id, drawback_status: status}).then((e) => {
        if(e.code == 1){
            scope.row.drawback_status = status;
        }
    })
}


const search = () => {
    baTable.table.filter = state.search;
    baTable.getIndex()?.then(() => {
        baTable.initSort()
        baTable.dragSort()
    })
}

const exportData = () => {
    exportDataApi(state.search).then((e) => {
        window.open(e.data)
    })
}


const changeAdmin = (admin_id) => {
    state.search.admin_id = admin_id;
    baTable.table.extend.admin_id = admin_id;
    baTable.table.filter = state.search;
    baTable.getIndex()?.then(() => {
        baTable.initSort()
        baTable.dragSort()
    })
    
}


let optButtons = defaultOptButtons([])


// 自定义一个新的按钮
let newButton: OptButton[] = [
    {
        render: 'basicButton', // 渲染方式:tipButton=带tip的按钮,confirmButton=带确认框的按钮,moveButton=移动按钮
        name: 'edit',
        title: 'edit',
        text: t('打款'),
        class: 'btn btn-small btn-sm ml-0 mt-0',
        icon: '',
        display: (row: TableRow, _field: TableColumn) => {
            return row.dk_status == 0 ? true : false;
        }
    },{
        render: 'confirmButton', // 渲染方式:tipButton=带tip的按钮,confirmButton=带确认框的按钮,moveButton=移动按钮
        // name: 'qx',
        // title: 'qx',
        text: t('取消'),
        class: 'btn btn-small btn-sm ml-0 mt-0',
        icon: '',
        display: (row: TableRow, _field: TableColumn) => { // 按钮是否显示，请返回布尔值
            return row.dk_status == 1 ? true : false;
        },
        popconfirm: {
            confirmButtonText: t('确定'),
            cancelButtonText: t('取消'),
            confirmButtonType: 'danger',
            title: t('确定取消打款操作吗') + '?',
        },
        click: (row: TableRow, _field: TableColumn) => {
            cancelApi({id: row.id}).then((_e) => {
                row.dk_status = 0;
                row.dk_time = null;
            })
        },
    }
    // ,{
    //     render: 'confirmButton',
    //     name: 'delete',
    //     title: '',
    //     text: t('删除'),
    //     type: 'danger',
    //     class: 'btn btn-danger mt-0 btn-small ml-5 ml--6',
    //     icon: '',
    //     popconfirm: {
    //         confirmButtonText: t('删除'),
    //         cancelButtonText: t('取消'),
    //         confirmButtonType: 'danger',
    //         title: t('确定要删除这条数据吗') + '?',
    //     }
    // }
]

// 新按钮合入到默认的按钮数组
optButtons = newButton.concat(optButtons)

// const optButtons: OptButton[] = defaultOptButtons(['edit', 'delete'])





/**
 * baTable 内包含了表格的所有数据且数据具备响应性，然后通过 provide 注入给了后代组件
 */
const baTable = new baTableClass(
    new baTableApi('/system/finance.Drawback/'),
    {
        pk: 'id',
        column: [
            { type: 'selection', align: 'center', operator: false },
            
            { label: t('下单时间'), prop: 'create_time', align: 'center', render: 'datetime', timeFormat: 'yy-mm-dd' },
            { label: t('订单号'), prop: 'out_trade_no', align: 'center', width: 165, render: 'slot', slotName: 'trade_type' },
            { label: t('订单'), prop: 'order_images', render: 'image', align: 'center' },
            { label: t('总价') + '(CNY)', prop: 'amount', align: 'center', operatorPlaceholder: t('Fuzzy query'), operator: 'LIKE', sortable: false },
            { label: t('货款'), prop: 'cost', align: 'center', operatorPlaceholder: t('Fuzzy query'), operator: 'LIKE', sortable: false },
            { label: t('退税率'), prop: 'drawback_rate', align: 'center', render: 'slot', slotName: 'drawback_rate' },
            { label: t('退税额'), prop: 'drawback_amount', align: 'center', operatorPlaceholder: t('Fuzzy query'), operator: 'LIKE', sortable: false },
            { label: t('已退税'), prop: 'refunded', align: 'center', render: 'customRender', customRender: markRaw(Refunded), width: 100 },
            { label: t('状态'), prop: 'dk_status', align: 'center', render: 'slot', slotName: 'dk_status' },
            { label: t('备注'), prop: 'drawback_note', align: 'center', render: 'customRender', customRender: markRaw(Note), width: 180 },
            // { label: t('操作时间'), prop: 'optime', align: 'center', render: 'slot', slotName: 'optime' },
            // { label: t('操作时间'), prop: 'optime', align: 'center' },
            // { label: t('操作'), align: 'center', render: 'buttons', buttons: optButtons, operator: false, width: 100 },
        ],
        dblClickNotEditColumn: ['all'],
        defaultOrder: { prop: 'id', order: 'desc' },
    },
    {
        defaultItems: { },
    },{
        requestEdit: ({ id }: { id: string }) => { // 编辑请求前
            
        }
    },{
        getIndex: ({ res }: { res: ApiResponse }) => { // 查看后
            console.log(res)
            state.drawback_amount_count = res.data.drawback_amount_count;
            state.drawback_amount_count_true = res.data.drawback_amount_count_true;
            state.drawback_amount_count_false = res.data.drawback_amount_count_false;
        },
        requestEdit: ({ res }: {res: ApiResponse}) => { // 编辑请求后
            
        },
    }
)

provide('baTable', baTable)

onMounted(() => {
    baTable.table.ref = tableRef.value
    baTable.mount()
    baTable.getIndex()?.then(() => {
        baTable.initSort()
        baTable.dragSort()
    })
    
})
</script>

<style scoped lang="scss">

:deep(.el-image){
    // width: 60px;
    // height: 60px;
    width: unset;
    height: unset;
    // max-height: 100px;
    // max-width: 80px;
    // max-height: 80px;
}
:deep(td.business-name > div.cell) {
    min-height: 80px;
    display:flex;
    justify-content:center; 
    align-items:center;
}
.tian{
    cursor: pointer;
}
.tian:hover{
    color: #409EFF;
}

:deep(.el-image > img){
    // width: 60px;
    // height: 60px;
    width: unset;
    height: unset;
    // max-height: 100px;
    max-width: 80px;
    max-height: 80px;
}
:deep(.ba-data-table .btn){
    padding: 4px 14px;
}


:deep(.ba-data-table .margin-left-none:last-child){
    margin-bottom: 0px;
}
:deep(.table-operate-text){
    padding-left: 0;
}
:deep(.font-size-13){
    font-size: 13px;
}
:deep(.pay-type .el-radio){
    margin-right: 10px;
}
pre{
    font-family: 'Helvetica Neue',Helvetica,'PingFang SC','Hiragino Sans GB','Microsoft YaHei','微软雅黑',Arial,sans-serif;
}
:deep(.ba-operate-dialog .el-dialog__body){
    padding-bottom: 0;
    padding-left: 0;
    padding-right: 0;
}
:deep(.quotation-scrollbar .el-scrollbar:first-child > .el-scrollbar__wrap){
    height: 100%;
}
:deep(.ml-0){
    margin-left: 0!important;
}
:deep(.mt-8){
    margin-top: 8px;
}
:deep(.ml--6){
    margin-left: -6px;
}
:deep(.ml-5){
    margin-left: 5px;
}
:deep(.mt-5){
    margin-top: 5px;
}
:deep(.el-button){
    height: unset;
}
:deep(.el-table__row.no-hover:hover .no-hover) {
  background-color: transparent!important; /* 使用 !important 来确保覆盖默认样式 */
  color: inherit !important;
//   background-color: red;
}
.tag{
    margin-right: 18px;
}

// .tag .active{
//     background: #ECF5FF;
//     color: #409EFF;
//     border: 1px solid #C6E2FF;
// }
:deep(.information .el-drawer__body){
    padding-top: 0;
}
:deep(.upload-column .el-form-item__label){
    display: none;
}
:deep(.upload-column .ba-upload.file){
    line-height: 8px;
}
:deep(.tag-0 .el-table td.el-table__cell, .el-table th.el-table__cell.is-leaf){
    border-top: 2px solid #efefef;
    border-right: 2px solid #efefef;

}

:deep(.information .el-table th.el-table__cell){
    background: rgb(245, 245, 246);
    color: #333;
    border-color: rgb(234, 234, 234);
}
:deep(.information .el-form--inline .el-form-item){
    margin-right: 23px;
}
:deep(.information .el-timeline-item){
    padding-bottom: unset;
}
:deep(.information .el-timeline-item__timestamp){
    font-size: 14px;
}
:deep(.el-button){
    height: unset;
}

:deep(.el-image > img){
    max-width: 50px;
    max-height: 50px;
}
:deep(.el-form--default.el-form--label-top .el-form-item .el-form-item__label){
    margin-bottom: 3px;
}
:deep(.information .el-drawer__header){
    margin-bottom: 17px!important;
}
:deep(.ml--6){
    margin-left: -1px;
}
</style>
